<template>
  <div class="production-management">
    <page>
      <div slot="body">
        <div>
          <div class="line"></div>
          <!-- <div style="font-size: 15px; font-weight: 600">产品总生产目标</div>
          <el-form class="form" label-width="140px" inline>
            <el-form-item class="form-item" label="设计总需求量：" prop="name">
              <el-input v-model="total" placeholder="" disabled size="small" />
              <i class="el-icon-edit" @click="edit(1)"></i>
            </el-form-item>
            <el-form-item class="form-item" label="设计完成时间：" prop="name">
              <el-input v-model="time" placeholder="" disabled size="small" />
              <i class="el-icon-edit" @click="edit(2)"></i>
            </el-form-item>
          </el-form> -->
          <div style="text-align: right;">
            <el-radio-group v-model="value1" @input="radioInput">
              <el-radio-button v-for="(item, index) in list1" :label="item.code" :key="index">{{item.name}}</el-radio-button>
            </el-radio-group>
          </div>
        </div>
        <div style="margin-bottom:  10px;">
          <el-button type="primary" size="small" icon="el-icon-plus" @click="handleAdd"> 添加产品 </el-button>
        </div>
        <el-table :data="tableData" border>
          <el-table-column label="序号" type="index" width="50px"></el-table-column>
          <el-table-column label="产品编号" prop="prodCode"></el-table-column>
          <el-table-column label="产品类型" prop="prodType">
            <template slot-scope="scope">
              <span>{{
                (list1.find(item => item.code == scope.row.prodType) || {}).name ||
                scope.row.prodType
              }}</span>
            </template>
          </el-table-column>
          <el-table-column v-if="value1=='ggj'" label="弧长" prop="arcLength"></el-table-column>
          <el-table-column v-if="value1=='ggj'" label="弦高" prop="height"></el-table-column>
          <el-table-column v-if="value1=='ggj'" label="半径" prop="radius"></el-table-column>
          <el-table-column v-if="value1=='ggj'" label="工字钢规格" prop="steel"></el-table-column>

          <el-table-column v-if="value1=='wp'" label="长" prop="arcLength"></el-table-column>
          <el-table-column v-if="value1=='wp'" label="宽" prop="prodWide"></el-table-column>
          <el-table-column v-if="value1=='wp'" label="钢筋规格" prop="rebarSpecifications"></el-table-column>

          <el-table-column v-if="value1=='ljg'" label="长" prop="arcLength"></el-table-column>
          <el-table-column v-if="value1=='ljg'" label="宽" prop="prodWide"></el-table-column>
          <el-table-column v-if="value1=='ljg'" label="厚" prop="proThick"></el-table-column>
          <el-table-column v-if="value1=='ljg'" label="钢板规格" prop="steelPlateSpecifications"></el-table-column>

          <el-table-column v-if="value1=='xdg'" label="长" prop="arcLength"></el-table-column>
          <el-table-column v-if="value1=='xdg'" label="止浆段长度" prop="prodLong"></el-table-column>
          <el-table-column v-if="value1=='xdg'" label="小导管规格" prop="conduitSpecifications"></el-table-column>
          <!-- <el-table-column label="规格" prop="tunnelArea">
            <template slot-scope="scope">
              <span>{{
                (statusOptions.find(item => item.dictValue == scope.row.unit) || {}).dictLabel || scope.row.tunnelArea
              }}</span>
            </template>
          </el-table-column>
          <el-table-column label="单元" prop="unit">
            <template slot-scope="scope">
              <span>{{
                (unitList.find(item => item.dictValue == scope.row.unit) || {}).dictLabel || scope.row.unit
              }}</span>
            </template>
          </el-table-column>
          <el-table-column label="大样图" prop="image" show-overflow-tooltip></el-table-column> -->
          <el-table-column label="操作" width="100px">
            <template slot-scope="scope">
              <el-link type="primary" @click="handleEdit(scope.row)" :underline="false">编辑</el-link>
              <el-link type="primary" @click="handleDelete(scope.row)" :underline="false">删除</el-link>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="140px">
          <el-form-item label="设计总需求量：" prop="prodNum" v-if="num == 1">
            <el-input v-model="form.prodNum" placeholder="设计总需求量：" />
          </el-form-item>
          <el-form-item label="设计完成时间：" prop="finishDate" v-if="num == 2">
            <el-date-picker v-model="form.finishDate" type="date" placeholder="选择日期" value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
      <el-dialog :title="prodTitle" :close-on-click-modal="false" :visible.sync="openProd" width="800px" append-to-body>
        <el-form ref="prodForm" :model="prodForm" :rules="prodRules" label-width="140px">
          <el-form-item label="产品编号：" prop="prodCode">
            <el-input v-model="prodForm.prodCode"  placeholder="请输入产品编号" />
          </el-form-item>
          <el-form-item v-if="value1=='ggj'" label="弧长：" prop="arcLength">
            <el-input v-model="prodForm.arcLength" type="number" placeholder="请输入弧长" />
          </el-form-item>
          <el-form-item v-if="value1=='ggj'" label="弦高：" prop="height">
            <el-input v-model="prodForm.height" type="number" placeholder="请输入弦高" />
          </el-form-item>
          <el-form-item v-if="value1=='ggj'" label="半径：" prop="radius">
            <el-input v-model="prodForm.radius" type="number" placeholder="请输入半径" />
          </el-form-item>
          <el-form-item v-if="value1=='ggj'" label="工字钢规格：" prop="steel">
            <el-input v-model="prodForm.steel" placeholder="请输入工字钢规格" />
          </el-form-item>
          <div v-if="value1=='ggj'" style="font-weight: bold;">切割机</div>
          <el-form-item v-if="value1=='ggj'" label="材料厚度：" prop="clhd">
            <el-input v-model="prodForm.clhd" type="number" placeholder="请输入材料厚度" />
          </el-form-item>
          <el-form-item v-if="value1=='ggj'" label="半径(中)：" prop="bj">
            <el-input v-model="prodForm.bj" type="number" placeholder="请输入半径(中)" />
          </el-form-item>
          <el-form-item v-if="value1=='ggj'" label="外弧长：" prop="whc">
            <el-input v-model="prodForm.whc" type="number" placeholder="请输入外弧长" />
          </el-form-item>
          <el-form-item v-if="value1=='ggj'" label="规格" prop="pf">
            <el-input v-model="prodForm.pf" placeholder="请输入规格" />
          </el-form-item>
          <el-form-item v-if="value1=='ggj'" label="设定产量" prop="sdcl">
            <el-input v-model="prodForm.sdcl" type="number" placeholder="请输入设定产量" />
          </el-form-item>
          <div style="font-weight: bold;" v-if="value1=='ggj'">焊接机</div>
          <el-form-item v-if="value1=='ggj'" label="外弦长" prop="wxc">
            <el-input v-model="prodForm.wxc" type="number" placeholder="请输入外弦长" />
          </el-form-item>
          <el-form-item v-if="value1=='ggj'" label="规格" prop="pf1">
            <el-input v-model="prodForm.pf1" placeholder="请输入规格" />
          </el-form-item>
          <el-form-item v-if="value1=='ggj'" label="设定产量" prop="sdcl1">
            <el-input v-model="prodForm.sdcl1" type="number" placeholder="请输入设定产量" />
          </el-form-item>
          <el-form-item v-if="value1=='wp'" label="长：" prop="arcLength">
            <el-input v-model="prodForm.arcLength" type="number" placeholder="请输入长" />
          </el-form-item>
          <el-form-item v-if="value1=='wp'" label="宽：" prop="prodWide">
            <el-input v-model="prodForm.prodWide" type="number" placeholder="请输入宽" />
          </el-form-item>
          <el-form-item v-if="value1=='wp'" label="钢筋规格：" prop="rebarSpecifications">
            <el-input v-model="prodForm.rebarSpecifications" placeholder="请输入钢筋规格" />
          </el-form-item>
          <el-form-item v-if="value1=='wp'" label="距离1：" prop="jl1">
            <el-input v-model="prodForm.jl1" placeholder="请输入距离1" />
          </el-form-item>
          <el-form-item v-if="value1=='wp'" label="次数1：" prop="cs1">
            <el-input v-model="prodForm.cs1" placeholder="请输入次数1" />
          </el-form-item>
          <el-form-item v-if="value1=='wp'" label="距离2：" prop="jl2">
            <el-input v-model="prodForm.jl2" placeholder="请输入距离2" />
          </el-form-item>
          <el-form-item v-if="value1=='wp'" label="次数2：" prop="cs2">
            <el-input v-model="prodForm.cs2" placeholder="请输入次数2" />
          </el-form-item>
          <el-form-item v-if="value1=='wp'" label="距离3：" prop="jl3">
            <el-input v-model="prodForm.jl3" placeholder="请输入距离3" />
          </el-form-item>
          <el-form-item v-if="value1=='wp'" label="次数3：" prop="cs3">
            <el-input v-model="prodForm.cs3" placeholder="请输入次数3" />
          </el-form-item>
          <el-form-item v-if="value1=='wp'" label="距离4：" prop="jl4">
            <el-input v-model="prodForm.jl4" placeholder="请输入距离4" />
          </el-form-item>
          <el-form-item v-if="value1=='wp'" label="次数4：" prop="cs4">
            <el-input v-model="prodForm.cs4" placeholder="请输入次数4" />
          </el-form-item>
          <el-form-item v-if="value1=='wp'" label="距离5：" prop="jl5">
            <el-input v-model="prodForm.jl5" placeholder="请输入距离5" />
          </el-form-item>
          <el-form-item v-if="value1=='wp'" label="次数5：" prop="rebarSpecifications">
            <el-input v-model="prodForm.cs5" placeholder="请输入次数5" />
          </el-form-item>
          <el-form-item v-if="value1=='wp'" label="剪网次数：" prop="jwcs">
            <el-input v-model="prodForm.jwcs" placeholder="请输入剪网次数" />
          </el-form-item>
          <el-form-item v-if="value1=='wp'" label="送丝长度：" prop="sscd">
            <el-input v-model="prodForm.sscd" placeholder="请输入送丝长度" />
          </el-form-item>
          <el-form-item v-if="value1=='wp'" label="当前网格：" prop="dqwg">
            <el-input v-model="prodForm.dqwg" placeholder="请输入当前网格" />
          </el-form-item>
          <el-form-item v-if="value1=='wp'" label="拉网坐标：" prop="lwzb">
            <el-input v-model="prodForm.lwzb" placeholder="请输入拉网坐标" />
          </el-form-item>
          <el-form-item v-if="value1=='ljg'" label="长：" prop="arcLength">
            <el-input v-model="prodForm.arcLength" placeholder="请输入长" />
          </el-form-item>
          <el-form-item v-if="value1=='ljg'" label="宽：" prop="prodWide">
            <el-input v-model="prodForm.prodWide" placeholder="请输入宽" />
          </el-form-item>
          <el-form-item v-if="value1=='ljg'" label="厚：" prop="proThick">
            <el-input v-model="prodForm.proThick" placeholder="请输入厚" />
          </el-form-item>
          <el-form-item v-if="value1=='ljg'" label="钢板规格：" prop="steelPlateSpecifications">
            <el-input v-model="prodForm.steelPlateSpecifications" placeholder="请输入钢板规格" />
          </el-form-item>
          
          <el-form-item v-if="value1=='xdg'" label="长：" prop="arcLength">
            <el-input v-model="prodForm.arcLength" type="number" placeholder="请输入长" />
          </el-form-item>
          <el-form-item v-if="value1=='xdg'" label="止浆段长度：" prop="prodLong">
            <el-input v-model="prodForm.prodLong" placeholder="请输入止浆段长度" />
          </el-form-item>
          <el-form-item v-if="value1=='xdg'" label="小导管规格：" prop="conduitSpecifications">
            <el-input v-model="prodForm.conduitSpecifications" placeholder="请输入小导管规格" />
          </el-form-item>
          <el-form-item v-if="value1=='xdg'" label="钢管总长：" prop="ggzc">
            <el-input v-model="prodForm.ggzc" type="number" placeholder="请输入钢管总长" />
          </el-form-item>
          <el-form-item v-if="value1=='xdg'" label="成品长度：" prop="cpcd">
            <el-input v-model="prodForm.cpcd" type="number" placeholder="请输入成品长度" />
          </el-form-item>
          <el-form-item v-if="value1=='xdg'" label="成品1割孔组数：" prop="cp1gkzs">
            <el-input v-model="prodForm.cp1gkzs" placeholder="请输入成品1割孔组数" />
          </el-form-item>
          <el-form-item v-if="value1=='xdg'" label="成品1割孔孔数：" prop="cp1gkks">
            <el-input v-model="prodForm.cp1gkks" placeholder="请输入成品1割孔孔数" />
          </el-form-item>
          <el-form-item v-if="value1=='xdg'" label="成品1尖头长度：" prop="cp1jtcd">
            <el-input v-model="prodForm.cp1jtcd" placeholder="请输入成品1尖头长度" />
          </el-form-item>
          <el-form-item v-if="value1=='xdg'" label="钢管直径：" prop="ggzj">
            <el-input v-model="prodForm.ggzj" type="number" placeholder="请输入钢管直径" />
          </el-form-item>
          <el-form-item v-if="value1=='xdg'" label="割孔半径：" prop="gkbj">
            <el-input v-model="prodForm.gkbj" type="number" placeholder="请输入割孔半径" />
          </el-form-item>
          <el-form-item v-if="value1=='xdg'" label="毛坯尾料长度：" prop="mpwlcd">
            <el-input v-model="prodForm.mpwlcd" type="number" placeholder="请输入毛坯尾料长度" />
          </el-form-item>
          <el-form-item v-if="value1=='xdg'" label="割枪休息位：" prop="gqxxw">
            <el-input v-model="prodForm.gqxxw" placeholder="请输入割枪休息位" />
          </el-form-item>
          <el-form-item v-if="value1=='xdg'" label="成品1尾料长度：" prop="steelPlateSpecifications">
            <el-input v-model="prodForm.steelPlateSpecifications" placeholder="请输入成品1尾料长度" />
          </el-form-item>
          <el-form-item v-if="value1=='xdg'" label="切割速度" prop="cp1wlcd">
            <el-input v-model="prodForm.cp1wlcd" placeholder="请输入切割速度" />
          </el-form-item>
          <!-- <el-form-item label="隧道部位：" prop="tunnelArea">
            <el-select v-model="prodForm.tunnelArea" placeholder="请选择隧道部位">
              <el-option v-for="dict in statusOptions" :key="dict.id" :label="dict.dictLabel" :value="dict.dictValue" />
            </el-select>
          </el-form-item>
          <el-form-item label="工字钢类型：" prop="steel">
            <el-input v-model="prodForm.steel" placeholder="请输入工字钢类型" />
          </el-form-item>
          <el-form-item label="单元：" prop="unit">
            <el-select v-model="prodForm.unit" placeholder="请选择单元">
              <el-option v-for="dict in unitList" :key="dict.id" :label="dict.dictLabel" :value="dict.dictValue" />
            </el-select>
          </el-form-item> -->
          <!-- <el-form-item label="上传图片：">
            <ImageUpload ref="EnterRef" :limit=1></ImageUpload>
          </el-form-item> -->
          <el-form-item label="上传文件：">
            <el-upload
              :action="uploadFileUrl"
              :file-list="fileList"
              :limit="1"
              :on-error="handleUploadError"
              :on-exceed="handleExceed"
              :on-success="handleUploadSuccess"
              :headers="headers"
              class="upload-file-uploader"
              ref="upload"
            >
              <!-- 上传按钮 -->
              <el-button size="mini" type="primary">选取文件</el-button>
            </el-upload>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitProdForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
      <div slot="footer">
        <el-pagination
          background
          :page-size="pageVO.pageSize"
          :current-page.sync="pageVO.pageNum"
          :total="pageVO.total"
          :layout="pageOptions.layout"
          :page-sizes="pageOptions.pageSizes"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </page>
  </div>
</template>

<script>
import { getProdPlan, addProdPlan, getList, getDicts, updateProd, deleteProd, getprolist } from '@/api/product';
import { getToken } from '@/utils/auth';
import pageQuery from '../../mixins/pageQuery';
import ImageUpload from "@/components/ImageUpload/index.vue";
export default {
  mixins: [pageQuery],
  components: { ImageUpload },
  data() {
    return {
      form: {
        prodNum: '',
        finishDate: '',
        id: '',
      },
      list1: [],
      value1: 'ggj',
      prodForm: {},
      open: false,
      openProd: false,
      total: '',
      time: '',
      title: '',
      prodTitle: '',
      tableData: [],
      areaList: [],
      statusOptions: [],
      unitList: [],
      num: 0,
      rules: {
        prodNum: [{ required: true, message: '设计总需求量不能为空', trigger: 'blur' }],
        finishDate: [{ required: true, message: '设计完成时间不能为空', trigger: 'blur' }],
      },
      prodRules: {
        tunnel: [{ required: true, message: '请输入所属隧道', trigger: 'blur' }],
        tunnelArea: [{ required: true, message: '请选择隧道区域', trigger: 'chage' }],
        steel: [{ required: true, message: '请输入工字钢类型', trigger: 'blur' }],
        unit: [{ required: true, message: '请选择单元', trigger: 'chage' }],
        prodCode: [{ required: true, message: '请输入产品编号', trigger: 'blur' }],
        // upload:[
        //   { required: true, message: "", trigger: "chage" }
        // ]
      },
      uploadFileUrl: process.env.VUE_APP_BASE_API + '/steel-arch/client/file/upload', // 上传的图片服务器地址
      headers: {
        Authorization: 'Bearer ' + getToken(),
      },
      fileList: [],
    };
  },
  created() {
    this.getprolist()
    this.getplan();
    this.getTableData();
    const { projectId } = this.$store.getters;
    this.prodForm.projectId = projectId;
    getDicts({ dictType: 'sys_tunnel_area' }).then(res => {
      this.statusOptions = res.data.data;
    });
    getDicts({ dictType: 'sys_prod_unit' }).then(res => {
      this.unitList = res.data.data;
    });
  },
  methods: {
    getplan() {
      const { projectId } = this.$store.getters;
      getProdPlan(projectId).then(res => {
        this.total = res.data.prodNum;
        this.time = res.data.finishDate;
        (this.form.prodNum = res.data.prodNum), (this.form.finishDate = res.data.finishDate);
        this.form.id = res.data.id;
      });
    },
    getprolist() {
      getprolist().then(res =>{
        if(res.success == true){
          this.list1 = res.data
        }
      })
    },
    radioInput(e){
      this.getTableData()
    },
    getTableData() {
      const { projectId } = this.$store.getters;
      const parmas = {
        projectId: projectId,
        prodType: this.value1,
      };
      getList(parmas).then(res => {
        this.tableData = res.data;
        this.pageVO.total = res.data.length
      });
    },
    edit(num) {
      this.open = true;
      if (num == 1) {
        this.title = '设计总需求量修改';
        this.num = 1;
      } else {
        this.title = '设计完成时间修改';
        this.num = 2;
      }
    },
    submitForm: function () {
      this.$refs['form'].validate(valid => {
        if (valid) {
          const { projectId } = this.$store.getters;
          this.form.projectId = projectId;
          this.form.prodType = this.value1
          
          addProdPlan(this.form).then(response => {
            this.$message.success('修改成功');
            this.open = false;
            this.getplan();
            this.getTableData();
          });
        }
      });
    },
    submitProdForm: function () {
      this.$refs['prodForm'].validate(valid => {
        if (valid) {
          const { projectId } = this.$store.getters;
          this.prodForm.projectId = projectId;
          this.prodForm.prodType = this.value1;
          // this.prodForm.image = this.$refs.EnterRef.getUrl();
          if (this.prodForm.id != null) {
            updateProd(this.prodForm).then(response => {
              this.$message.success('修改成功');
              this.openProd = false;
              this.getTableData();
            });
          } else {
            updateProd(this.prodForm).then(response => {
              this.$message.success('新增成功');
              this.openProd = false;
              this.getTableData();
            });
          }
        }
      });
    },
    cancel() {
      this.open = false;
      this.$refs.prodForm.resetFields();
      this.openProd = false;
    },
    handleAdd() {
      this.openProd = true;
      this.prodTitle = '添加产品';
      this.prodForm={}
      this.prodForm.image = ''
      // this.$nextTick(() => {this.$refs.EnterRef.setUrl("");})
      
    },
    // 文件个数超出
    handleExceed() {
      this.$message.error(`只允许上传单个文件`);
    },
    // 上传失败
    handleUploadError(err) {
      this.$message.error('上传失败, 请重试');
    },
    // 上传成功回调
    handleUploadSuccess(res, file) {
      this.$message.success('上传成功');
      this.prodForm.image = res.data;
    },
    // 删除文件
    handleDelete(index) {
      //this.fileList.splice(index, 1);
    },
    handleEdit(row) {
      this.openProd = true;
      this.prodForm = JSON.parse(JSON.stringify(row));
      this.prodTitle='编辑产品';
      const { projectId } = this.$store.getters;
      this.prodForm.projectId = projectId;
      console.log(this.$refs.EnterRef)
      // this.fileList.push({url:this.prodForm.image})
      // this.$nextTick(() => {this.$refs.EnterRef.setUrl(this.prodForm.image);})
      
    },
    handleDelete(row) {
      this.$confirm('是否确认删除产品ID为"' + row.prodType + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(function () {
          return deleteProd(row.id);
        })
        .then(() => {
          this.getTableData();
          this.$message.success('删除成功');
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.top {
  display: flex;
  justify-content: flex-start;
  margin: 20px 0;
}

.line {
  width: 8px;
  background: #1890ff;
  margin-right: 15px;
}
.form {
  display: flex;
  margin-top: 10px;
  ::v-deep {
    .el-form-item__label {
      font-weight: normal;
    }
  }
}
.form-item {
  position: relative;
}
.el-icon-edit {
  font-size: 20px;
  right: 10px;
  position: absolute;
  top: 8px;
}
</style>
